package controllers

import (
	"blog_api/models"
	"github.com/gin-gonic/gin"
	"strconv"
	"strings"
)

type CategoryController struct {
	BaseController
}

/**
 * IndexCatTree
 * @Description: 首页栏目分类树
 * @Author: Jazk-Z
 * @receiver cc
 * @param c
 */
func (cc CategoryController) IndexCatTree(c *gin.Context) {
	cat_info := models.CategoryTree()
	cc.success(c, "success", cat_info)
	return
}

/**
 * IndexCatBreadcrumb
 * @Description: 首页面包屑
 * @Author: Jazk-Z
 * @receiver cc
 * @param c
 */
func (cc CategoryController) IndexCatBreadcrumb(c *gin.Context) {
	cid, _ := strconv.Atoi(c.DefaultQuery("cid", "0"))
	lid, _ := strconv.Atoi(c.DefaultQuery("lid", "0"))

	cb := models.CatBreadcrumb(cid, lid)
	cc.success(c, "success", cb)
	return
}

/**
 * AdminCategoryList
 * @Description: 管理后台——分类栏目列表
 * @Author: Jazk-Z
 * @receiver l
 * @param c
 */
func (l CategoryController) AdminCategoryList(c *gin.Context) {
	keyword := strings.TrimSpace(c.DefaultQuery("keyword", ""))
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	limit, _ := strconv.Atoi(c.DefaultQuery("limit", "13"))
	offset := (page - 1) * limit

	clist, count := models.AdminCategoryList(limit, offset, "*", keyword)
	data := make(map[string]interface{})
	data["list"] = clist
	data["count"] = count
	l.success(c, "success", data)
	return
}
